package interview;

import java.util.*;

public class Demo1 {
    static List<User> list = Arrays.asList(
            new User("lisi", 19),
            new User("lisi", 20),
            new User("zsan", 14),
            new User("lsi", 23),
            new User("wwu", 46),
            new User("zsan", 29));


    public static void main1(String[] args) {
        Set<User> set = new TreeSet<>(Comparator.comparing(User::getName));
        Set<User> set2 = new TreeSet<>(Comparator.comparing(User::getAge));
        set.addAll(list);
        set2.addAll(list);
        set.forEach(System.out::println);
        System.out.println();
        set2.forEach(System.out::println);
    }

    public static void main(String[] args) {
        Set<User> set = new HashSet<>(list);
        System.out.println(set);
    }
}

class User implements Comparable<User>{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof User){
            return this.name.equals(((User) obj).name);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return 1;
    }

    @Override
    public int compareTo(User o) {
        return 0;
    }
}